<template>
  <!-- template只允许一个子容器 -->
  <div>
    <!-- 对话框开始 -->
    <!--  title：对话框标题 :visible.sync:绑定是否显示的变量  -->
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <!-- 表单开始内容 -->
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="品牌">
          <el-select v-model="form.brand.id" placeholder="请选择品牌">
            <el-option v-for="brand in brands" :key="brand.id" :label="brand.name" :value="brand.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="form.type" style="width: 65%" clearable/>
        </el-form-item>
        <el-form-item label="制程">
          <el-col :span="12">
            <el-input v-model="form.lithography" maxlength="3">
              <template slot="append">纳米</template>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="最大频率">
          <el-col :span="14">
            <el-input v-model="form.maxTurboFrequency" maxlength="4">
              <template slot="append">GHz</template>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="核心数" style="width: 10px">
          <el-input-number v-model="form.coreNumber" :min="1" :max="100"/>
        </el-form-item>
        <el-form-item label="线程数">
          <el-input-number v-model="form.threadNumber" :min="1" :max="100"/>
        </el-form-item>
        <el-form-item label="销售价格">
          <el-col :span="12">
            <el-input v-model="form.customerPrice" type="" maxlength="3">
              <template slot="append">元</template>
            </el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <!-- 表单结束 -->
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 对话框结束 -->
    <el-col>
      <el-input placeholder="查找型号" v-model="searchText" clearable style="width: 40%"/>
      <el-button type="primary" @click="showAdd">添加CPU信息</el-button>
    </el-col>
    <el-table
      :data="cpuInfo.filter(data=>!searchText||data.type.toLowerCase()
      .includes(searchText.toLowerCase()))"
      border style="width: 100%"
    >
      <el-table-column prop="brand.name" label="品牌" width="180"/>
      <el-table-column prop="type" label="型号" width="180"/>
      <el-table-column prop="lithography" label="制程" :formatter="formatLithography"/>
      <el-table-column prop="maxTurboFrequency" label="最大频率" :formatter="formatFrequency"/>
      <el-table-column prop="coreNumber" label="核心数"/>
      <el-table-column prop="threadNumber" label="线程数"/>
      <el-table-column prop="customerPrice" label="销售价格"/>
      <el-table-column prop="stock" label="库存"/>
      <el-table-column label="操作" min-width="140px" fixed="right">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="primary" icon="el-icon-edit" @click="showEdit(scope.$index)"></el-button>
            <el-button type="primary" icon="el-icon-delete" @click="del(scope.$index)"></el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      cpuInfo: [
        { id: 0, type: '', lithography: '', maxTurboFrequency: '', customerPrice: 0 }
      ],
      brands: [],
      dialogVisible: false,
      form: {
        id: 0, brand: { id: 1, name: '' }, type: '', lithography: '',
        maxTurboFrequency: '', coreNumber: 1, threadNumber: 1, customerPrice: ''
      },
      searchText: ''
    }
  },
  created() {
    this.refreshDateAndCloseDialog()
  },
  methods: {
    refreshDateAndCloseDialog() {
      const that = this
      this.$http.get('/cpu/all').then(e => {
        that.cpuInfo = e.data
      })
      this.$http.get('/cpu/brand/all').then(e => {
        that.brands = e.data
      })
    },
    showMessage(bool, message) {
      this.$message({
        message: message,
        type: bool === true ? 'success' : 'error'
      })
      this.dialogVisible = false
    },
    formatLithography(column) {
      return column.lithography + '纳米'
    },
    formatFrequency(row, column, cellValue) {
      return cellValue + 'GHz'
    },
    // 显示编辑页面
    showEdit(index) {
      const editId = this.cpuInfo[index].id
      this.$http.get('/cpu/' + editId).then(v => {
        this.form = v.data
      }).then(() => this.dialogVisible = true)
    },
    // 确定编辑/更新
    save() {
      let flag = false
      const that = this
      this.$http.put('/cpu/' + this.form.id, this.form).then(result => {
        if (result.status === 200) {
          flag = true
          this.showMessage(true, this.form.id === 0 ?
            '添加成功' : '更新成功')
          this.refreshDateAndCloseDialog()
        }
      })
      setTimeout(function() {
        if (flag === false) {
          that.showMessage(false, '连接错误！')
          that.dialogVisible = false
        }
      }, 2500)
    },
    del(index) {
      const delId = this.cpuInfo[index].id
      this.$http.delete('/cpu/' + delId).then(_ => {
        this.showMessage(true, '删除成功')
        this.refreshDateAndCloseDialog()
      })
    },
    showAdd() {
      this.form = {
        id: 0, brand: { id: 1, name: '' }, type: '', lithography: '',
        maxTurboFrequency: '', coreNumber: 1, threadNumber: 1, customerPrice: '', stock: 0
      }
      this.dialogVisible = true
    }
  }
}
</script>
